<?
class tableNameDAL {
	public function Insert($db, $tableNameObj, &$errorMsg) {	
		$errorMsg = "";	

		$query = "INSERT INTO tableNames (Field01, Field02) VALUES (".ToSQLPostVAL($tableNameObj->Field01).", ".ToSQLPostVAL($tableNameObj->Field02).")";	
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";		
			return false;
		}

		$tableNameObj->tableNameID = mysql_insert_id();	
		return true;
	}
	public function Update($db, $tableNameObj, &$errorMsg) {	
		$errorMsg = "";	

		$query = "UPDATE tableName SET Field01=".ToSQLPostVAL($tableNameObj->Field01).", Field02=".ToSQLPostVAL($tableNameObj->Field02)." WHERE FieldID=".ToSQLPostVAL($tableNameObj->FieldID);	
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";		
			return false;
		}

		return true;
	}

	public function Load($db, $id, &$errorMsg) {
		$errorMsg = "";	
		$query	= "SELECT * FROM tableName WHERE FieldID='$id'";		

		if (!$db->query($query)) {		
			$errorMsg	= mysql_error()." Query: $query.";		
			return null;
		} else {		
			if ($db->next_record()) {	
				return new tableNameObj($db->f("FieldID"), $db->f("Field01"), $db->f("Field02"));
			} else {
				$errorMsg = "Data not found.";
				return null;
			}
		}	
		return false;
	}

	public function IsExist($db, $whereStr) {
		$query	= "SELECT * FROM tableName";		
		
		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else return false;

		if (!$db->query($query)) {		
			return false;	
		} else {		
			while ($db->next_record()) 	{
				return true;
			}
		}	
		return false;
	}

	public function Pure($db, $whereStr, &$errorMsg) {
		$errorMsg = "";	

		$query = "DELETE FROM tableName";	
		if ($whereStr!="") $query = $query." WHERE ".$whereStr;

		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";		
			return false;
		}

		return true;
	}


	public function GetList($db, $whereStr, $orderByStr, $limit, &$errorMsg) {
		$returnData = array();
		$errorMsg = "";	
		$query	= "SELECT * FROM tableName";		
		
		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		if ($orderByStr!="") $query = $query." ORDER BY ".$orderByStr;
		if ($limit!="") $query = $query." LIMIT ".$limit;

		if (!$db->query($query)) {		
			$errorMsg	= mysql_error()." Query: $query.";		
		} else {		
			while ($db->next_record()) 	{
				$returnData[] = new tableNameObj($db->f("FieldID"), $db->f("Field01"), $db->f("Field02"));
			}
		}	
		return $returnData;
	}

	public function Pop($tableNameObj) {
		global $FieldID, $Field01, $Field02;

		if (isset($FieldID)) $tableNameObj->FieldID = $FieldID;
		if (isset($Field01) $tableNameObj->Field01 = $Field01;
		if (isset($Field02)) $tableNameObj->Field02 = $Field02;
	}

	public function Push($tableNameObj) {
		global $FieldID, $Field01, $Field02;

		$FieldID = $tableNameObj->FieldID; 
		$Field01 = $tableNameObj->Field01;
		$Field02 = $tableNameObj->Field02;
	}
}
?>